package com.luyou.awt.image;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;


/**
 * 手绘程序
 */
public class HandDraw {

    //画图区的宽度、高度
    private final int AREA_WIDTH = 500;
    private final int AREA_HEIGHT = 400;

    //鼠标拖动事件的坐标
    private int perX = -1;
    private int perY = -1;

    //定义一个菜单用于设置画笔
    PopupMenu pop = new PopupMenu();
    MenuItem redItem = new MenuItem("红色");
    MenuItem greenItem = new MenuItem("绿色");
    MenuItem blueItem = new MenuItem("蓝色");
    MenuItem cleanItem = new MenuItem("清空");
    //定义一个bufferedImage对象
    BufferedImage image = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_BGR);
    Graphics g = image.getGraphics();

    private JFrame frame = new JFrame("简单手绘程序");
    private DrawCanvas drawArea = new DrawCanvas();
    //保存画笔
    private Color foreColor = Color.red;
    private void init(){
        ActionListener menuListener = e ->{
            switch (e.getActionCommand()) {
                case "绿色":
                    foreColor = Color.GREEN;
                    break;
                case "红色":
                    foreColor = Color.RED;
                    break;
                case "蓝色":
                    foreColor = Color.BLUE;
                    break;
            }
        };
        redItem.addActionListener(menuListener);
        greenItem.addActionListener(menuListener);
        blueItem.addActionListener(menuListener);
        cleanItem.addActionListener( e ->{
            //清空
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT);
            drawArea.repaint();
        });
        pop.add(redItem);
        pop.add(greenItem);
        pop.add(blueItem);
        pop.add(cleanItem);
        //右键菜单添加到drawArea对象中
        drawArea.add(pop);
        //将image对象的背景色填充成白色
        g.fillRect(0, 0, AREA_WIDTH, AREA_HEIGHT);
        drawArea.setPreferredSize(new Dimension(AREA_WIDTH, AREA_HEIGHT));

        //监听鼠标的移动动作
        drawArea.addMouseMotionListener(new MouseMotionAdapter() {
            //鼠标按下拖动
            @Override
            public void mouseDragged(MouseEvent e) {
                if(perX > 0 && perY > 0){
                    g.setColor(foreColor);
                    g.drawLine(perX, perY, e.getX(), e.getY());
                }
                perX = e.getX();
                perY = e.getY();
                drawArea.repaint();
            }
        });
        drawArea.addMouseListener(new MouseAdapter() {
            //鼠标松开
            @Override
            public void mouseReleased(MouseEvent e) {
                //弹出右键
                if(e.isPopupTrigger()){
                    pop.show(drawArea, e.getX(), e.getY());
                }
                //松开鼠标后将上一次鼠标拖动的X Y改成-1
                perX = -1;
                perY = -1;
            }
        });
        frame.add(drawArea);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new HandDraw().init();
    }

    /**
     *
     */
    class DrawCanvas extends Canvas{
        //重写paint实现绘画
        @Override
        public void paint(Graphics g) {
            g.drawImage(image, 0, 0, null);
        }
    }
}
